﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using RaSuperGrid.SuperGrid.ViewModel;
using RaSuperGrid.SuperGrid.Converter;
using RaSuperGrid.XMLService;

namespace RaSuperGrid.SuperGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PRCollectionViewModel _vm;

        public MainWindow()
        {
            InitializeComponent();

            _vm = new PRCollectionViewModel();
            this.DataContext = _vm;
        }

        private void PreviewContactInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !ContactValueConverter.IsValidContactInput(e.Text);
        }

        private void PastingContactInput(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(string)))
            {
                string data = (string)e.DataObject.GetData(typeof(string));
                if (!ContactValueConverter.IsValidContact(data))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            }
        }

        private void PreviewCostInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !CostValueConverter.IsValidCostInput(e.Text);
        }

        private void PastingCostInput(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(string)))
            {
                string data = (string)e.DataObject.GetData(typeof(string));
                if (!CostValueConverter.IsValidCostInput(data))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            } 
        }

        private void PreviewReqPersonInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !NameValueConverter.IsValidName(e.Text);
        }

        private void PastingReqPersonInput(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(string)))
            {
                string data = (string)e.DataObject.GetData(typeof(string));
                if (!NameValueConverter.IsValidName(data))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            } 
        }

        private void prDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            if (e.AddedCells.Count == 0) return;
            var currentCell = e.AddedCells[0];
            var sortMemberPath = (string)currentCell.Column.SortMemberPath;

            switch (sortMemberPath)
            {
                case "ReqPerson":
                case "Urgency":
                case "Reason":
                case "NeedDate":
                case "Special":
                case "Contact":
                case "Cost":
                    prDataGrid.BeginEdit();
                    break;
            }
        }

        private void reqDateButton_Click(object sender, RoutedEventArgs e)
        {
            var button = (Button)e.Source;

            if (button != null)
            {
                reqDatePopup.PlacementTarget = button;
                reqDatePopup.DataContext = button.DataContext;

                if (reqDatePopup.IsOpen)
                    reqDatePopup.IsOpen = false;

                reqDatePopup.IsOpen = true;
            }
        }

        private void descriptionButton_Click(object sender, RoutedEventArgs e)
        {
            var button = (Button)e.Source;

            if (button != null)
            {
                descriptionPopup.PlacementTarget = button;
                descriptionPopup.DataContext = button.DataContext;

                if (descriptionPopup.IsOpen)
                    descriptionPopup.IsOpen = false;

                descriptionPopup.IsOpen = true;
            }
        }

        private void specialButton_Click(object sender, RoutedEventArgs e)
        {
            var button = (Button)e.Source;

            if (button != null)
            {
                specialPopup.PlacementTarget = button;

                if (specialPopup.IsOpen)
                    specialPopup.IsOpen = false;

                specialPopup.IsOpen = true;
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            _vm.AddNew();
        }   
    }
}
